// result.dto.ts
import { ApiProperty } from '@nestjs/swagger';
import { ResultCode } from './ResultCode';

export class Result<T> {
  @ApiProperty({ description: '响应代码', example: 200 })
  code: string;

  @ApiProperty({ description: '响应消息', example: '操作成功' })
  msg: string;

  @ApiProperty({ description: '响应数据', type: Object })
  data: T;

  constructor(code: string, message: string, data?: T) {
    this.code = code;
    this.msg = message;
    this.data = data;
  }

  static success<T>(data?: T): Result<T> {
    return new Result<T>(
      ResultCode.SUCCESS.getCode(),
      ResultCode.SUCCESS.getMsg(),
      data,
    );
  }

  static failed(result: Result<any>): Result<null> {
    return new Result<null>(result.code, result.msg, null);
  }

  static failedWithMsg(message: string): Result<null> {
    return new Result<null>('500', message, null);
  }

  static judge(flag: boolean) {
    if (flag) {
      return this.success();
    } else {
      return this.failed(ResultCode.SYSTEM_EXECUTION_ERROR);
    }
  }
}
